package cate.game.farm.local.cross_boss;

import cate.game.farm.local.cross_boss.data.*;
import cate.game.framework.ModuleMan;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import cp.solution.rpc.RpcMsgHead;
import easy.java.dev.note.NoteClass;

import java.util.Optional;

@NoteClass("跨服BOSS本服管理")
public class CrossBossManager extends ModuleMan {

    @Override
    public void initDependent() throws Exception {
        // 加载常量
        CrossBossConstants.loadFromTable(game.table.common);
        // 跨服监听
        game.rpcRecv().onFireAndForgetTemplate(CrossBossInnerMsg.BROADCAST_OVERALL, CrossBossOverallVO.class, this::onBroadcastOverall);
    }

    /**
     * 收到广播消息群发
     *
     * @param head 信息头
     * @param vo BOSS概览数据
     */
    private void onBroadcastOverall(RpcMsgHead head, CrossBossOverallVO vo) {
        game.role.activeForOnline(role -> role.getFarm().crossBoss.sendOverallInfo(vo));
    }

    /**
     * 跨服BOSS整体数据
     */
    public CrossBossOverallVO getCrossBossOverall() {
        return game.rpcSend().requestResponseCross(CrossBossInnerMsg.OVERALL_BOSS_INFO,
                null,
                CrossBossOverallVO.class);
    }

    /**
     * 跨服BOSS单个详细信息
     *
     * @param id   BOSS配置ID
     * @param role 角色
     */
    public CrossBossDetailVO getCrossBossDetail(int id, Role role) {
        return game.rpcSend().requestResponseCross(CrossBossInnerMsg.VIEW_BOSS_DETAIL,
                new CBIViewBossDetail(id, role.getUid()),
                CrossBossDetailVO.class);
    }

    /**
     * 尝试发起战斗
     *
     * @param id BOSS配置ID
     * @return 剩余血量 0=BOSS已死亡 -1=BOSS锁定或异常状态
     */
    public long tryFightCrossBoss(int id) {
        return Optional.ofNullable(game.rpcSend().requestResponseCross(CrossBossInnerMsg.TRY_FIGHT_BOSS,
                id,
                Long.class)).orElse(-1L);
    }

    /**
     * 上报战斗结果
     *
     * @param id     BOSS配置ID
     * @param role   角色
     * @param damage 伤害
     */
    public void reportFightResult(int id, Role role, long damage) {
        game.rpcSend().fireAndForgetCross(CrossBossInnerMsg.REPORT_FIGHT_RESULT,
                new CBIReportFightResult(id, new RoleSnapshot(role), damage));
    }
}
